<?php

namespace App\Http\Controllers;

use App\Models\Master;
use App\Models\Video;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class VideoController extends Controller
{
    public function index(){//视频列表
        $oVideos=Video::paginate(5);
        return view('admin.videolist')
            ->with('oVideos',$oVideos);
    }

    public function create(){//新增视频
        $oMasters =Master::all();
        return view('admin.videocreate')
            ->with('oMasters',$oMasters);
    }

    public function store(Request $request){//新增处理逻辑
        $aVideo=$request->input();//获取输入信息
        if(isset($aVideo['img_thumb'])){//检查$aVideo['img_thumb']是否被设置
            $aVideo["thumb"]=$aVideo['img_thumb'];
        }
        else{
            $aVideo["thumb"]='/assets/test_images/r1.jpg';
        }
        $aVideo['show']=$request->input('description');
        $Master=Master::find($aVideo['doc_id'][0]);//通过doc_id获取第一个专家
        $aVideo['master_id']=$Master->id;
        $video=Video::create($aVideo);
        foreach ($aVideo['doc_id'] as $v){
            $video->Comment()->attach((int)$v);
        }
        return redirect('/adm/video-list');


    }

    public function upload() {
        $allow_type=["image/jpeg","image/gif","image/png"];
        if(!in_array($_FILES['upload_file']['type'],$allow_type)){
            $msg='图片类型不符合要求';
            return json_encode(['success'=>false,'msg'=>$msg]);
        }
        elseif($_FILES['upload_file']['size']>2000000){
            $msg='图片大小不符合要求';
            return json_encode(['success'=>false,'msg'=>$msg]);
        }
        elseif($_FILES['upload_file']['error']!=0){
            $msg='上传出错';
            return json_encode(['success'=>false,'msg'=>$msg]);
        }
        else {
            $src_image=$_FILES['upload_file']['tmp_name'];//获取上传文件的信息和名字
            $size=getimagesize($_FILES['upload_file']['tmp_name']);
            $src_width=$size[0];
            $src_height=$size[1];
            $tar_width=264;
            $tar_height=155;
            $tar_thumb=imagecreatetruecolor($tar_width,$tar_height);//设置图片大小创建彩色图片资源
            if($_FILES['upload_file']['type']=='image/jpeg'){
                $src_thumb=imagecreatefromjpeg($src_image);
            }
            elseif ($_FILES['upload_file']['type']=="image/gif"){
                $src_thumb=imagecreatefromgif($src_image);
            }
            else{
                $src_thumb=imagecreatefrompng($src_image);//从png图像读取图片资源
            }

            imagecopyresized($tar_thumb,$src_thumb,0,0,0,0, $tar_width,$tar_height,$src_width,$src_height);//缩放并复制
            $path='./assets/images/video_upload/'.$_FILES['upload_file']['name'];//路径
            imagejpeg($tar_thumb,$path);//保存为JPEG格式
            return json_encode(['success'=>true,'img_thumb'=>'/assets/images/video_upload/'.$_FILES['upload_file']['name']]);
        }
    }

    public function edit($id){//修改页面
        $oMasters=Master::all();
        $oVideo=Video::find($id);
        return view('admin.videoedit')
            ->with('oVideo',$oVideo)
            ->with('oMasters',$oMasters);
    }


    public function update(Request $request,$id){//修改处理逻辑
        $video=Video::find($id);
        $aVideo=$request->input();
        if(isset($aVideo['img_thumb'])){
            $aVideo["thumb"]=$aVideo['img_thumb'];
        }
        else{
            $aVideo["thumb"]=$video->thumb;
        }
        $Master=Master::find($aVideo['doc_id'][0]);//
        $aVideo['master_id']=$Master->id;
        $video->update($aVideo);
        foreach ($aVideo['doc_id'] as $v){
            $video->Comment()->attach((int)$v);
        }
        return redirect('/adm/video-list');
    }

    public function destroy($id){//删除

        $oVideo=Video::find($id);
        $oVideo->delete();
        return redirect('/adm/video-list');


    }
}
